package com.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.setting.PersonInfo;
import com.org.service.PersonService;

@Controller
@RequestMapping("/person")
public class PersonAction {
	/**
	 * 负责人信息初始化，录入及展示
	 */
	@Autowired
	private PersonService personService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<PersonInfo> personInfo) { // 封装成一个Json文件返回
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (personInfo.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", personInfo);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	private Map<String, Object> status(Boolean personInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (personInfo) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getDepartInfos() {
		List<PersonInfo> personInfo = new ArrayList<PersonInfo>();
		personInfo = personService.findAll();
		return status(personInfo);
	}

	@RequestMapping("/{personName}/findContact")
	public @ResponseBody  Map<String, Object> getContact(@PathVariable String personName){
		List<PersonInfo> personInfo = new ArrayList<PersonInfo>();
		personInfo=personService.findContact(personName);
		return status(personInfo);
		
	}
	@RequestMapping("{personId}/findById")
	public @ResponseBody Map<String, Object> getPersonInfo(
			@PathVariable int personId) {
		List<PersonInfo> personInfo = new ArrayList<PersonInfo>();
		personInfo = personService.findById(personId);
		return status(personInfo);

	}

	@RequestMapping("/addPerson")
	public @ResponseBody Map<String, Object> addPerson(
			@RequestBody Map<String, Object> personMap) {
		boolean result;
		iter = personMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		PersonInfo personInfo = new PersonInfo();
		personInfo.setPersonName((String) personMap.get(list.get(0)));
		personInfo.setContact((String) personMap.get(list.get(1)));
		personInfo.setAdRegion((String) personMap.get(list.get(2)));
		personInfo.setRemark((String) personMap.get(list.get(3)));
		result = personService.addPerson(personInfo);
		return status(result);
	}

	@RequestMapping("/updatePerson")
	public @ResponseBody Map<String, Object> updatePerson(
			@RequestBody Map<String, Object> personMap) {
		boolean result;
		iter = personMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		PersonInfo personInfo = new PersonInfo();
		personInfo.setPersonName((String) personMap.get(list.get(0)));
		personInfo.setContact((String) personMap.get(list.get(1)));
		personInfo.setAdRegion((String) personMap.get(list.get(2)));
		personInfo.setRemark((String) personMap.get(list.get(3)));
		result = personService.addPerson(personInfo);
		return status(result);
	}

	@RequestMapping("/{personId}/deletePerson")
	public @ResponseBody Map<String, Object> deletePerson(
			@PathVariable int personId) {
		boolean result;
		result = personService.deletePerson(personId);
		return status(result);

	}

}
